<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
    private $articletype="";
    public function __construct(){
        parent::__construct();
        //获取分类信息
        $articletype = M('articletype')->select();
        $i = 0;
        foreach ($articletype as $a) {
            $articletype[$i]['nums'] = M('article')->where(array(
                'type' => $a['id']
            ))->count();
            $i ++;
        }
        $this->articletype=$articletype;
    }
    
    //首页
    public function index(){

        $where=array();
        if(I('type')){
            $id=I('type');
            $where['type']=$id;
        }
        $article=M('article')->order("time desc")->where($where)->select();
        //查出分类的名称
        foreach ($article as $key=>$val){
            $type=M('articletype')->where(array('id'=>$val['type']))->getField('name');
            if(!$type||$type==""){
                $type="未分类";
            }
            $article[$key]['type']=$type;
        }
        $this->assign('articletype',$this->articletype);
		$this->assign('article',$article);
		$this->display();
     
    }
    
    //查看文章
    public function veiw(){
        $id=I('id');
        $model=M('article');
        $article=$model->where(array('id'=>$id))->find();
        $ip=$_SERVER['REMOTE_ADDR'];
        if(!M('articleveiw')->where(array('ip'=>$ip,'pid'=>$id))->find()&&$ip!=""){
          
            $model->where(array('id'=>$id))->save(array('veiw'=>$article['veiw']+1));//阅读数

            M('articleveiw')->add(array(
                'ip'=>$ip,
                'pid'=>$id,
                'time'=>time()
            ));
        }
        //查出分类的名称

            $type=M('articletype')->where(array('id'=>$article['type']))->getField('name');
            if(!$type||$type==""){
                $type="未分类";
            }

            $article['type']=$type;
       // dump($article);exit;
        $reply=M('articlecomment')->where(array('articleid'=>$id))->select();
        $article['replynums']=count($reply);
       
        $this->assign('articlecomment',$reply);
        $this->assign('articletype',$this->articletype);
        $this->assign("article",$article);
        $this->display();
    }
    
    //个人中心
    public function user(){
        $username=session("username");
        if(!username){
            $this->error('请选登录');
        }else{
            $user=M('user')->where(array('username'=>$username))->find();
            $this->assign('articletype',$this->articletype);
            $this->assign('user',$user);
            $this->display();
        }
        
    }
    
    //发表文章
    public function report(){
        $this->checkpower();
        if(IS_POST){
            $_POST['time']=date("Y-m-d H:i:s",time());
            $_POST['author']=session('username');
        
            if(M('article')->add($_POST)){
                $this->success("发表成功",U('Home/Index/index'));
            }
        }else{
            $this->assign('articletype',$this->articletype);
            $this->display();
        }
    }
    
    //发表回复
    public function reply(){
        if(!session('username')){
            $this->error("请先登录",U("Login/index"));
        }else{
            $_POST['content']=htmlspecialchars($_POST['content']);
            M('articlecomment')->add($_POST);
            $this->success("评论成功");
        }
    }
    
    //退出登录
    public function logout(){
        session(null);
        $this->success("退出成功");
    }
    
    //检查是否拥有回复的权限
    public function getpower(){
        $user=session('username');
        if($user&&$power=M('user')->where(array('username'=>$user))->getField("power")){
            return $power;
        }else {
            $this->error('没有权限',U('Home/Login/index'));
        }
    }
    //检查是否拥有编辑的权限
    public function checkpower(){
        $power=$this->getpower();
        if($power<2){
            $this->error('权限不足',U('Home/Login/index'));
        }
    }
    
    //编辑
    public function edit(){
        $this->checkpower();
        if(IS_POST){
            if(M('article')->save($_POST)){
                $this->success("修改成功");
            }else{
                $this->error("修改失败");
            }
        }else{
            if(IS_GET){
                $id=I('id');
                $where['id']=$id;
            }
            $article=M('article')->where($where)->find();
            $this->assign('articletype',$this->articletype);
            $this->assign('article',$article);
            $this->display();
        }
    }
}
